<?php
namespace Admin\Model;
use Think\Model;

class ContracttemplateModel extends AbstractModel {

    public $_validate = array(
        array('Title','require','请输入合同名称',Model::MUST_VALIDATE),
        array('Content','require','请输入合同内容',Model::MUST_VALIDATE),
        array('CatID','number','请选择合同分类',Model::MUST_VALIDATE)
    );
    //获取合同列表
    public function MyshowList(){
        $where = "A.IsDel=0";
        $keyword = I('keyword');
        if(!empty($keyword)){
            $where .= " AND Title like '%$keyword%' ";
        }
        $return = array();
        $sql = " SELECT COUNT(*) AS count FROM contracttemplate AS A LEFT JOIN contracttemplatecatalog AS B ON(A.CatID=B.ID) WHERE $where ";
        $count = $this->query($sql);
        $count = $count[0]['count'];
        $page = $this->Mypage($count, 8);
        $sql = "SELECT A.ID,Title,Author,Content,CatID,PubTime,`Name` FROM contracttemplate AS A LEFT JOIN contracttemplatecatalog AS B ON(A.CatID=B.ID) WHERE $where ORDER BY A.ID DESC LIMIT  $page->firstRow , $page->listRows";
        $res = $this->query($sql);
        $return['list'] = $res;
        $return['page'] = $page->show('Admin');
        return $return;
    }


    public function Myshow($parmArr){
        $where = " A.isDel!=1 ";
        foreach($parmArr AS $key => $value){
            $where .= " AND A.$key = '$value'"; 
        }
        $sql = "SELECT A.ID,Title,Author,Content,CatID,PubTime,Name FROM contracttemplate AS A LEFT JOIN  contracttemplatecatalog AS B ON(A.CatID=B.ID)  WHERE $where";
        $res = $this->query($sql);
        return $res;
    }


    public function Myupdata(){
        if(!$this->create()){
            $results['msg'] = $this->getError();
            $results['state'] = 0;
        }else{
            $res = $this->save();
            if($res) {
                $results['status'] = 1;
                $results['msg'] = "修改成功！";
                $results['id'] = $res;
            }else{
                $results['status'] = 0;
                $results['msg'] = "修改错误！";
            }

        }
        return $results;
    }

   /**
     * 删除业务
     */
    public function MydeletP(){
        $dbName = $this->getTableName();
        $ID = I("ID");
        $sql = " UPDATE {$dbName} SET IsDel = 1 WHERE ID = {$ID}";
        $res = $this->execute($sql);
        if($res){
            $results['state'] = 1;
            $results['msg'] = "删除成功！";
            $results['id'] = $res;
        }else{
            $results['state'] = 0;
            $results['msg'] = "删除错误！";
        }
        return $results;
    }
    
    
    //获取分类
    public function GetCatalog(){
        $sql='SELECT * FROM contracttemplatecatalog';
        $res = $this->query($sql);
        $return['selectlis'] = $res;
        return $return;
    }

    //预览
    public function templatec(){
        $ID = I('ID');
        $sql="SELECT * FROM contracttemplate where ID=$ID";
        $res = $this->query($sql);
        $return = $res[0];
        return $return;
    }

    //获取模板标签
    public function getTpList(){
        $array[]=array('id'=>'Title','name'=>'合同标题');
        $array[]=array('id'=>'CNO','name'=>'合同编号');
        $array[]=array('id'=>'Bill','name'=>'合同总金额');
        $array[]=array('id'=>'YearBill','name'=>'合同年金额');
        $array[]=array('id'=>'MonthBill','name'=>'合同月金额');
        $array[]=array('id'=>'Bond','name'=>'合同保证金');
        $array[]=array('id'=>'Memo','name'=>'备注');

        $array[]=array('id'=>'UserName','name'=>'合同签订人');
        $array[]=array('id'=>'LeaderName','name'=>'合同签约领导');
        $array[]=array('id'=>'DepName','name'=>'承办部门');
        $array[]=array('id'=>'CreateDate','name'=>'合同创建日期');

        /*甲方信息*/
        $array[]=array('id'=>'FPName','name'=>'甲方名称');
        $array[]=array('id'=>'FPAddress','name'=>'甲方地址');
        $array[]=array('id'=>'FPContact','name'=>'甲方联系人');
        $array[]=array('id'=>'FPPhone','name'=>'甲方联系电话');
        $array[]=array('id'=>'FPBank','name'=>'甲方开户银行');
        $array[]=array('id'=>'FPBNO','name'=>'甲方银行账号');
        $array[]=array('id'=>'FPBankName','name'=>'甲方银行账户名称');
        $array[]=array('id'=>'FPWriteDate','name'=>'甲方签字日期');
        /*乙方信息*/
        $array[]=array('id'=>'SPName','name'=>'乙方名称');
        $array[]=array('id'=>'SPAddress','name'=>'乙方地址');
        $array[]=array('id'=>'SPContact','name'=>'乙方联系人');
        $array[]=array('id'=>'SPPhone','name'=>'乙方联系电话');
        $array[]=array('id'=>'SPBank','name'=>'乙方开户银行');
        $array[]=array('id'=>'SPBNO','name'=>'乙方开户银行账号');
        $array[]=array('id'=>'SPBankName','name'=>'乙方银行账户名称');
        $array[]=array('id'=>'SPWriteDate','name'=>'乙方签字日期');

      
        return $array;

    }

    // 渲染合同元素
    function rederTP(){
        $ID=I('ContractID');
        $ContractType=I('ContractType');
        $Contract=D('contract');
        $sql1='SELECT
        A.Title AS Title,
        A.CNO  AS CNO,
        A.Bill AS Bill,
        A.YearBill AS YearBill,
        A.MonthBill AS MonthBill,
        A.Bond AS Bond,
        A.Memo AS Memo,
        (SELECT `user`.UserName FROM `user`  WHERE `user`.ID=A.UserID) AS UserName,
        (SELECT `user`.UserName FROM `user`  WHERE `user`.ID=A.LeaderUserID) AS LeaderName,
        (SELECT department.`Name` FROM department WHERE department.ID=A.DepID) AS DepName,
        A.CreateDate AS CreateDate,
        B.`Name` AS FPName,
        B.Address AS FPAddress,
        B.Contact AS FPContact,
        B.Phone AS FPPhone,
        B.Bank AS FPBank,
        B.BankNO AS FPBankNO,
        B.BankName AS FPBankName,
        A.FPWriteDate,
        C.`Name` AS SPName,
        C.Address AS SPAddress,
        (SELECT `user`.UserName FROM `user`  WHERE `user`.ID=C.BossID) AS SPContact,
        C.Phone AS SPPhone,
        C.Bank AS SPBank,
        C.BankNO AS SPBankNO,
        C.BankName AS SPBankName,
        A.SPWriteDate
        FROM
        contract A
        LEFT JOIN customer B ON (A.FPID = B.ID)
        LEFT JOIN department C ON (A.SPID = C.ID) where A.ID='.$ID;

        $sql2='SELECT
        A.Title AS Title,
        A.CNO  AS CNO,
        A.Bill AS Bill,
        A.YearBill AS YearBill,
        A.MonthBill AS MonthBill,
        A.Bond AS Bond,
        A.Memo AS Memo,
        (SELECT `user`.UserName FROM `user`  WHERE `user`.ID=A.UserID) AS UserName,
        (SELECT `user`.UserName FROM `user`  WHERE `user`.ID=A.LeaderUserID) AS LeaderName,
        (SELECT department.`Name` FROM department WHERE department.ID=A.DepID) AS DepName,
        A.CreateDate AS CreateDate,
        B.`Name` AS FPName,
        B.Address AS FPAddress,
        (SELECT `user`.UserName FROM `user`  WHERE `user`.ID=B.BossID)AS FPContact,
        B.Phone AS FPPhone,
        B.Bank AS FPBank,
        B.BankNO AS FPBankNO,
        B.BankName AS FPBankName,
        A.FPWriteDate,
        C.`Name` AS SPName,
        C.Address AS SPAddress,
        C.Contact AS SPContact,
        C.Phone AS SPPhone,
        C.Bank AS SPBank,
        C.BankNO AS SPBankNO,
        C.BankName AS SPBankName,
        A.SPWriteDate
        FROM
        contract A
        LEFT JOIN department B ON (A.FPID = B.ID)
        LEFT JOIN customer C ON (A.SPID = C.ID) where A.ID='.$ID;

        if($ContractType==0){
            $res=$Contract->query($sql1);
        }else{
            $res=$Contract->query($sql2);
        }

        $tpdata=$this->getTpList();
        //var_dump($res); exit();
        foreach ($tpdata as $key => $value) {
            
            $arr[]=array('id'=>$value['name'],'value'=>$res[0][$value['id']]);
        }

        return $arr;
    }


        //pdf 打水印预览
    public function pdf($ID){
        
        Vendor('mpdf.mpdf');
        $mpdf=new \mPDF('utf-8','A4','','',25,25,16,16);
        //html内容
        //字段含义按顺序分别为：  
        //$mode,$format,默认字体大小，默认字体，左页边距25（默认），右页边距（25），上页边距16，下页边距16，mgh:16,mgf:13,o
        //设置字体，解决中文乱码  
        $mpdf -> useAdobeCJK = TRUE;  
        $mpdf ->autoScriptToLang = true;  
        $mpdf -> autoLangToFont = true;  
        //$mpdf-> showImageErrors = true; //显示图片无法加载的原因，用于调试，注意的是,我的机子上gif格式的图片无法加载出来。  
        //设置pdf显示方式  
        $mpdf->SetDisplayMode('fullpage');  
        //水印
        $mpdf->SetWatermarkText('云上贵州复印无效',0.2);
        $mpdf->showWatermarkText=true;
        $mpdf->watermark_font = 'GB';
        //目录相关设置：  
        //Remember bookmark levels start at 0(does not work inside tables)H1 - H6 must be uppercase  
        //$this->h2bookmarks = array('H1'=>0, 'H2'=>1, 'H3'=>2);  
        $mpdf->h2toc = array('H3'=>0,'H4'=>1,'H5'=>2);  
        $mpdf->h2bookmarks = array('H3'=>0,'H4'=>1,'H5'=>2);  
        $mpdf->mirrorMargins = 1;  
        //是否缩进列表的第一级  
        $mpdf->list_indent_first_level = 0;  
          
        //导入外部css文件：  
        /*$stylesheet1 = file_get_contents(CSS_PATH.'target.css');  
        $mpdf->WriteHTML($stylesheet1,1);  */

        $url =  'http://'.$_SERVER['HTTP_HOST'].U('Admin/ContractTemplate/templatec',array('ID'=>$ID));
        $html = file_get_contents($url); 

        $mpdf->WriteHTML($html);  //$html中的内容即为变成pdf格式的html内容。  
        $fileName = '报告.pdf';  
        //输出pdf文件  
        $mpdf->Output($fileName,'I'); //'I'表示在线展示 'D'则显示下载窗口  
        exit; 
    }

}